<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="槽号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="slotId">
              <a-input v-model="model.slotId" placeholder="请输入槽号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="车间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workshopId">
              <j-dict-select-tag type="list" v-model="model.workshopId" dictCode="data_workshop,workshop_name,workshop_id" placeholder="请选择所属车间" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaId">
              <j-dict-select-tag type="list" v-model="model.areaId" dictCode="data_area,area_name,area_id" placeholder="请选择所属工区" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="语音" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupId">
              <a-input-number v-model="model.groupId" placeholder="请输入语音" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="起槽日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startDate">
              <j-date placeholder="请选择起槽日期" v-model="model.startDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="端口号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="port">
              <a-input-number v-model="model.port" placeholder="请输入端口号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="槽IP" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="netboxIp">
              <a-input v-model="model.netboxIp" placeholder="请输入槽IP"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="槽龄" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="age">
              <a-input v-model="model.age" placeholder="请输入槽龄"  ></a-input>
            </a-form-model-item>
          </a-col>
         <a-col :span="12">
            <a-form-model-item label="槽类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeId">
              <j-dict-select-tag type="list" v-model="model.typeId" dictCode="data_slot_type,type_name,type_id" placeholder="请选择槽类型" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="通道号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="netboxChannelId">
              <a-input v-model="model.netboxChannelId" placeholder="请输入通道号"  ></a-input>
            </a-form-model-item>
          </a-col> 
          <a-col :span="12">
            <a-form-model-item label="转换器" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="netboxId">
              <a-input-number v-model="model.netboxId" placeholder="请输入转换器" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
            <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status"> 
              <j-dict-select-tag placeholder="请选择槽状态" v-model="model.status" dictCode="data_slot_status"/>
            </a-form-model-item>
          </a-col>
           <!-- <a-col :span="24">
            <a-form-model-item label="转换器中对应槽号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="netboxSlotId">
              <a-input-number v-model="model.netboxSlotId" placeholder="请输入转换器中对应槽号" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'DataSlotForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          status:1,
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           slotId: [
              { required: true, message: '请输入槽号!'},
           ],
           workshopId: [
              { required: true, message: '请输入所属车间ID!'},
           ],
           startDate: [
              { required: true, message: '请输入起槽日期!'},
           ],
           port: [
              { required: true, message: '请输入端口号!'},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
           ],
           netboxIp: [
              { required: true, message: '请输入槽IP!'},
           ],
           status:[
              { required: true, message: '请选择状态!',trigger: 'change'},
           ],
        },
        url: {
          add: "/dataSlot/dataSlot/add",
          edit: "/dataSlot/dataSlot/edit",
          queryById: "/dataSlot/dataSlot/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>